import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";
import viteCompression from "vite-plugin-compression";
import { resolve } from "path";
import { sysInfo, envConfig } from "./config";
import { getProxyList } from "./config/proxy";

export default async ({ mode }) => {
  return defineConfig({
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        resolvers: [ElementPlusResolver(), IconsResolver({})],
      }),
      Components({
        resolvers: [
          IconsResolver({
            enabledCollections: ["ep"],
          }),
          ElementPlusResolver(),
        ],
      }),
      Icons({
        compiler: "vue3",
        autoInstall: true,
      }),
      viteCompression(),
    ],
    define: {
      APP_CONFIG: JSON.stringify({ ...envConfig[mode], ...sysInfo }),
    },
    resolve: {
      alias: {
        "@": resolve(__dirname, "./src"),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "@/styles/var.scss";`,
        },
      },
    },
    server: {
      host: "0.0.0.0",
      proxy: getProxyList(envConfig[mode]),
    },
  });
};
